package test.login;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserConf extends HttpServlet {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		doAttrivute(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO 自動生成されたメソッド・スタブ
		doAttrivute(req, resp);
	}

	protected void doAttrivute(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {

		// 画面入力情報取得
		String id = req.getParameter("s_id");
		String password = req.getParameter("s_password");
		String passwordr = req.getParameter("s_passwordr");
		String user = req.getParameter("s_user");
		String adminFlg = req.getParameter("s_adminflg");
		String inputFlg = req.getParameter("inputFlg");

		/* Beans格納 */
		User us = new User();

		us.setLoginId(id);
		us.setPassword(password);
		us.setPasswordr(passwordr);
		us.setName(user);
		us.setAdminFlg(adminFlg.charAt(0));

		/* 入力チェック開始 */
		boolean errFlg = false;
		String errMsg = "";

		/* 必須 */
		if (!InputCheck.Required(id) || !InputCheck.Required(password)
				|| !InputCheck.Required(user)) {
			errFlg = true;
			errMsg = "Required Error";
		}

		/* 半角英数 */
		if (!errFlg && !InputCheck.AlphabeticCharacters(id)) {
			errFlg = true;
			errMsg = "AlphabeticCharacters Error";
		}

		/* フォーマット */
		if (!errFlg && !InputCheck.Format(password)) {
			errFlg = true;
			errMsg = "Format Error";
		}

		/* 同値 */
		if (!errFlg && !InputCheck.Equivalence(password, passwordr)) {
			errFlg = true;
			errMsg = "Equivalence Error";
		}

		/* 文字長 */
		if (!errFlg
				&& (!InputCheck.CharacterLength(id, 10) || !InputCheck
						.CharacterLength(user, 20))) {
			errFlg = true;
			errMsg = "CharacterLength Error";
		}

		/* 入力チェック終了 */

		if ("1".equals(inputFlg)) {
			String targetId = req.getParameter("targetId");
			us.setUserId(Integer.valueOf(targetId));
		}
		req.setAttribute("userInfo", us);
		req.setAttribute("inputFlg", inputFlg);

		if (errFlg) {
			req.setAttribute("errMsg", errMsg);
			req.getRequestDispatcher("/WEB-INF/userInput.jsp").forward(req,
					resp);
		} else {
			req.getRequestDispatcher("/WEB-INF/userInputConf.jsp").forward(req,
					resp);
		}
	}
}
